﻿using System;
using System.Collections.Generic;
using FileMonitor.Configuration.Prototype;
using FileMonitor.Configuration.Providers;
using FileMonitor.Service.Prototype;

namespace FileMonitor.Service.Adapter
{
    public class FileMonitorProcessBaseAdpter
    {
        public static FileMonitorBase ConvertFromWorkProcessBase(WorkProcessBase workProcessBase)
        {
            Type baseType = workProcessBase.TypeValue;
            string processName = workProcessBase.Name;
            if (baseType==null)
            {
                throw new Exception("BaseType not be implemented!!!");
            }
            if (string.IsNullOrEmpty(processName))
            {
                throw new Exception("ProcessName can't be null!");
            }
            FileMonitorBase base1 = null;
            if (!typeof(FileMonitorBase).IsAssignableFrom(baseType))
            {
                throw new ArgumentException("Provider_must_implement_type:FileMonitorBase");
            }
            base1 = (FileMonitorBase)Activator.CreateInstance(baseType);
            if (!string.IsNullOrEmpty(workProcessBase.ExtendableConfig["SplitArr"]))
            {
                workProcessBase.ExtendableConfig["SplitArr"] = ProviderService.GetProvider().GetAllSpecialValues()[workProcessBase.ExtendableConfig["SplitArr"]];
            }
            base1.Initialize(processName, workProcessBase.ExtendableConfig);
            
            base1.BizProcess.CopyBizProcesses(workProcessBase.BizProcesses);


            if (!string.IsNullOrEmpty(workProcessBase.ExtendableConfig["TargetDatabaseName"]))
            {
                string TargetDatabaseName = workProcessBase.ExtendableConfig["TargetDatabaseName"];
                Dictionary<string, FileUtility.SqlDataBaseExtender> dicDbs = ProviderService.GetProvider().GetAllDataBase();
                if (dicDbs.ContainsKey(TargetDatabaseName))
                {
                    base1.exchangeDB = dicDbs[TargetDatabaseName];
                }
            }
            
            return base1;
        }
    }
}
